package com.tjetc.service.impl;

import com.tjetc.dao.CategoryMapper;
import com.tjetc.domain.Admin;
import com.tjetc.domain.Category;
import com.tjetc.service.CategoryService;
import com.tjetc.utils.Db;
import com.tjetc.vo.Result;

import java.util.List;

public class CategoryServiceImpl implements CategoryService {
    private CategoryMapper categoryMapper= Db.getMapper(CategoryMapper.class);
    @Override
    public Result list() {
        return new Result(200,"成功",categoryMapper.list());
    }

    @Override
    public Result add(Category category) {
        Category category1=categoryMapper.get(null,category.getName());
        if (category1==null){
            int rows=categoryMapper.add(category);
            if (rows==1){
                return new Result(200,"添加成功",null);
            }else {
                return new Result(500,"添加失败",null);
            }
        }else {
            return new Result(500,"已经拥有这个类",null);
        }
    }

    @Override
    public Result update(Category category) {
        Category category1=categoryMapper.get(category.getId(),null);
        if (category1!=null){
            int rows=categoryMapper.update(category);
            if (rows==1){
                return new Result(200,"修改成功",null);
            }else {
                return new Result(500,"修改失败",null);
            }
        }else {
            return new Result(500,"没有这个人",null);

        }
    }

    @Override
    public Result delete(int id) {
        Category category=categoryMapper.get(id,null);
        if (category!=null){
            int rows=categoryMapper.delete(id);
            if (rows==1){
                return new Result(200,"删除成功",null);
            }else {
                return new Result(500,"删除失败",null);
            }
        }else {
            return new Result(500,"没有这个人",null);

        }
    }
}
